import Vue from "vue";
import VueRouter from "vue-router";
import Layout from "../views/layout/Layout.vue";
import local from "../utils/local";
Vue.use(VueRouter);

const routes = [
  {
    path: "/",
    redirect: "/login",
  },
  // 首页
  {
    path: "",
    component: Layout,
    redirect: "/home",
    children: [
      {
        path: "/home",
        meta: { title: "首页" },
        component: () => import("../views/home/Home.vue"),
      },
    ],
  },

  // 账号管理
  {
    path: "/acc",
    component: Layout,
    redirect: "/acc/list",
    children: [
      {
        path: "/acc/list",
        meta: { title: "账号列表" },
        component: () => import("../views/account/AccontList.vue"),
      },
      {
        path: "/acc/add",
        meta: { title: "账号添加" },
        component: () => import("../views/account/Useradd.vue"),
      },
    ],
  },
];

const router = new VueRouter({
  routes,
});

// 全局路由前置守卫
router.beforeEach((to, form, next) => {
  //判断是否登录 在本地取token
  let isLogin = local.get("token") ? true : false;
  if (isLogin) {
    next(); //放行
  } else {
    // next({path:'/login'})
    if (to.path === "/login") {
      next();
    } else {
      next({ path: "/login" });
    }
  }
});

export default router;
